package com.demo.java.OD301_350.OD316;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【矩形相交的面积(B卷)】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/146106128
 */
public class OdMain {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Point> points = new ArrayList<>();

        // 输入三个矩形的坐标和尺寸
        for (int i = 0; i < 3; i++) {
            int x1 = scanner.nextInt();
            int y1 = scanner.nextInt();
            int w = scanner.nextInt();
            int h = scanner.nextInt();
            int x2 = x1 + w;  // 右上角的x坐标
            int y2 = y1 - h;  // 右上角的y坐标
            points.add(new Point(x1, y1, x2, y2));
        }

        // 提取所有矩形的边界坐标
        List<Integer> xs = new ArrayList<>();
        List<Integer> ys = new ArrayList<>();
        for (Point point : points) {
            xs.add(point.x1);
            xs.add(point.x2);
            ys.add(point.y1);
            ys.add(point.y2);
        }

        // 计算坐标的最小值和最大值
        int min_x = min(xs);
        int max_x = max(xs);
        int min_y = min(ys);
        int max_y = max(ys);

        // 初始化二维矩阵，记录每个位置的覆盖次数
        int[][] dp = new int[max_x - min_x + 1][max_y - min_y + 1];

        // 更新矩阵，标记被矩形覆盖的位置
        for (Point point : points) {
            int x1 = point.x1 - min_x;  // 归一化坐标
            int y1 = point.y1 - min_y;
            int x2 = point.x2 - min_x;
            int y2 = point.y2 - min_y;
            // 标记矩形区域
            for (int i = Math.min(x2, x1); i < Math.max(x2, x1); i++) {
                for (int j = Math.min(y2, y1); j < Math.max(y2, y1); j++) {
                    dp[i][j]++;
                }
            }
        }

        // 计算交集区域的面积
        int ret = 0;
        for (int i = 0; i < dp.length; i++) {
            for (int j = 0; j < dp[0].length; j++) {
                if (dp[i][j] == 3) {
                    ret++;  // 每个同时被三个矩形覆盖的格子计为1
                }
            }
        }

        // 输出交集面积
        System.out.println(ret);
    }

    // 求最小值
    static int min(List<Integer> list) {
        int minVal = Integer.MAX_VALUE;
        for (int num : list) {
            if (num < minVal) {
                minVal = num;
            }
        }
        return minVal;
    }

    // 求最大值
    static int max(List<Integer> list) {
        int maxVal = Integer.MIN_VALUE;
        for (int num : list) {
            if (num > maxVal) {
                maxVal = num;
            }
        }
        return maxVal;
    }

    // 矩形的类，存储矩形的左下角和右上角坐标
    static class Point {
        int x1, y1, x2, y2;

        Point(int x1, int y1, int x2, int y2) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }
    }
}